//
//  RTreeUtils.cpp
//  map-matching
//
//  Created by Lv Xin on 16/5/13.
//  Copyright © 2016年 Lv Xin. All rights reserved.
//
//  Personal Homepage: www.davidlvxin.com

#include <stdio.h>
#include "RTreeUtils.h"

RTree<Rect, double, 2, float>::Iterator it;
vector<Rect> searchRTreeResult;

bool MySearchCallback(Rect id, void* arg) {
    int keyword = ((Rect*)arg)->id;
    if ( keyword == -1 || keyword == id.id )
        searchRTreeResult.push_back(id);
    return true;
}

void CreateRTree(vector<struct Rectd> rects, RTree<Rect, double, 2, float> &tree) {
    int nrects = rects.size();
    for (int i = 0; i < nrects; i++) {
        tree.Insert(rects[i].min, rects[i].max, rects[i].vid);
    }
}

void SearchRTree(struct Rectd search_rect, RTree<Rect, double, 2, float> &tree) {
    searchRTreeResult.clear();
    int nhits = tree.Search(search_rect.min, search_rect.max, MySearchCallback,
                            &search_rect.vid);
}

void TraverseRTree(RTree<Rect, double, 2, float> &tree) {
    int count = 0;
    Rect value;
    for (tree.GetFirst(it); !tree.IsNull(it); tree.GetNext(it)) {
        value = tree.GetAt(it);
        double boundsMin[2] = { 0, 0 };
        double boundsMax[2] = { 0, 0 };
        it.GetBounds(boundsMin, boundsMax);
        count++;
    }
}